<?php
require_once("./define/define_table.php");

define("ADD_FREQUENCE_FORM", "Formulaire d'ajout de frequence");
define("MODIFY_FREQUENCE_FORM", "Formulaire de modification de frequence");

class Frequence {

	private $id_frequence;
	private $nom;

	public function Frequence() {

	}

	public function getNom(){
		return $this->nom;
	}
	
	public function getId(){
		return $this->id_frequence;
	}
	
	public function setNom($nom){
		$this->nom = $nom;
	}
	
	public function setId($id){
		$this->id_frequence = $id;
	}
	
	public function getAllFrequence() {
		global $db;
		try {
			$sql = "SELECT id_frequence, nom FROM " .FREQUENCE_TABLE;
			$sth = $db->prepare($sql);
			$sth->execute();
			PrintQuery::queryPrint($sth);
			return $sth->fetchAll(PDO::FETCH_CLASS, "Frequence");
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}

	public function getFrequence($id_frequence){
		global $db;
		try{
			$sql = "SELECT id_frequence, nom FROM ".FREQUENCE_TABLE." WHERE id_frequence = :id_frequence";
			$sth = $db->prepare($sql);
			$sth->bindValue(":id_frequence", $id_frequence, PDO::PARAM_INT);
			$sth->execute();
			return $sth->fetchObject("Frequence");
		}catch(PDOException $e){
			echo $e->getMessage();
		}
	}
	
	public function addOrModify(){
		global $db;
		try{
			if($this->id_frequence == null){
				$sql = "INSERT INTO ".FREQUENCE_TABLE." VALUES(null, :nom)";
				$sth = $db->prepare($sql);
				$sth->bindValue(":nom", $this->nom, PDO::PARAM_STR);
			}
			else{
				$sql = "UPDATE ".FREQUENCE_TABLE." SET nom = :nom WHERE id_frequence= :id_frequence ";
				$sth = $db->prepare($sql);
				$sth->bindValue(":nom", $this->nom, PDO::PARAM_STR);
				$sth->bindValue(":id_frequence", $this->id_frequence, PDO::PARAM_INT);
			}
			PrintQuery::queryPrint($sth);
			return $sth->execute(); 
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}
	
	public static function getAddForm(){
		$html = ADD_FREQUENCE_FORM.
				'<form class="ajax_form" id="add_frequence_form" action="?module=frequence&action=doAdd" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>NOM</td>' .
				'			<td><input type="text" name="nom" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
		return $html;
	}
	
	public function getModifyForm(){
		$html = MODIFY_FREQUENCE_FORM.
				'<form class="ajax_form" id="modify_frequence_form" action="?module=frequence&action=doModify" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>ID</td>' .
				'			<td><input readonly type="text" name="id_frequence" value = "'.$this->id_frequence.'" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>NOM</td>' .
				'			<td><input type="text" name="nom" value = "'.$this->nom.'" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
		return $html;
	}
	
	public function delete(){
	    global $db;
	    $sql = "DELETE FROM ".FREQUENCE_TABLE." WHERE id_frequence = :id_frequence";
	    $sth = $db->prepare($sql);
	    $sth->bindValue(":id_frequence", $this->id_frequence, PDO::PARAM_INT);
	    $sth->execute();
	    PrintQuery::queryPrint($sth);
	}
	
	public function generateSelect($id = null){
	    $array = self::getAllFrequence();
	    $html = '<select name="id_frequence">';
	    foreach($array as $f){
	        $selected = ($f->getId() == $id) ?  'selected="selected"' : '';
	        $html .= '<option value="'.$f->getId().'" '.$selected.'>'.$f->getNom().'</option>';
	    }
	    $html .= '</select>';
	    return $html;
	}
}
?>
